package controller;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import dto.Exposer;
import dto.SeckillExecution;
import dto.SeckillResult;
import entity.Seckill;
import enums.SeckillStateEnum;
import exception.RepeatKillException;
import exception.SeckillCloseException;
import service.SeckillService;
@Controller
@RequestMapping("/seckill")
public class SeckillController {
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private SeckillService seckillService;
	
	@RequestMapping(value = "/list",method=RequestMethod.GET)
	public String list(Model model){
		//获取详情列表页
		List<Seckill> list = seckillService.getSeckillList();
		model.addAttribute("list",list);
		return "list";  // 代表/WEB-INF/jsp/list.jsp
	}
	
	@RequestMapping(value = "/{seckillId}/detail", method=RequestMethod.GET)
	public String detail(@PathVariable("seckillId") Long seckillId, Model model){
		if(seckillId == null || "".equals(seckillId)){
			return "redirect:/seckill/list";
		}
		Seckill seckill = seckillService.queryById(seckillId);
		if(seckill == null){
			return "forward:/seckill/list";
		}
		model.addAttribute("seckill", seckill);
		return "detail";
	}
	
	//ajax json
	@ResponseBody
	@RequestMapping(value = "/{seckillId}/exposer", 
			method = RequestMethod.POST,
			produces = {"application/json;charset=UTF-8"})
	public SeckillResult<Exposer> exposer(@PathVariable("seckillId") Long seckillId, Model model){
		 SeckillResult<Exposer> result;
		 try {
			 Exposer exposer = seckillService.exportSeckillUrl(seckillId);
			 result = new SeckillResult<Exposer>(true,exposer);
		} catch (Exception e) {
			 logger.error(e.getMessage(),e);
			 result = new SeckillResult<Exposer>(false,e.getMessage());
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/{seckillId}/{md5}/execution", 
			method = RequestMethod.POST,
			produces = {"application/json;charset=UTF-8"})
	public SeckillResult<SeckillExecution> execution(@PathVariable("seckillId") Long seckillId, 
								@PathVariable("md5") String md5, 
								@CookieValue(value = "killPhone", required = false)Long userPhone,
								Model model){
		if(userPhone == null || "".equals(userPhone)){
			return new SeckillResult<SeckillExecution>(false,"未注册");
		}
		SeckillResult<SeckillExecution> result;
		try {
			//存储过程调用
			SeckillExecution dto = seckillService.excuteSeckillProcedure(seckillId, userPhone, md5);
			//SeckillExecution dto = seckillService.excuteSeckill(seckillId, userPhone, md5);
			result = new SeckillResult<SeckillExecution>(true,dto);
			return result;
		} catch (RepeatKillException e) {
			SeckillExecution dto = new SeckillExecution(seckillId, SeckillStateEnum.REPEAT_KILL);
			return new SeckillResult<SeckillExecution>(true,dto);
			
		} catch (SeckillCloseException e) {
			SeckillExecution dto = new SeckillExecution(seckillId, SeckillStateEnum.END);
			return new SeckillResult<SeckillExecution>(true,dto);
			
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			SeckillExecution dto = new SeckillExecution(seckillId, SeckillStateEnum.INNOR_ERROR);
			return new SeckillResult<SeckillExecution>(true,dto);
			
		}
	}
	@ResponseBody
	@RequestMapping(value = "/time/now", method = RequestMethod.GET)
	public SeckillResult<Long> time(){
		Date now = new Date();
		return new SeckillResult<Long>(true,now.getTime());
	}
}
